import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store";
Vue.use(VueRouter);

const router = new VueRouter({
    routes: [{
            path: "/",
            redirect: "/login"
        },

        {
            path: "/login",
            name: "login",
            component: () =>
                import ( /* webpackChunkName: "basepage" */ "@/pages/login/index")
        },
        {
            path: "/yunzhi",
            component: () =>
                import ("@/pages/modules/index"),
            redirect: "/menu",
            name: "系统管理",
            meta: { title: "系统管理", icon: "gwb" },
            children: [{
                    path: "/menu",
                    name: "菜单管理",
                    component: () =>
                        import ("@/pages/modules/yunzhi/menu/index"),
                    meta: { title: "菜单管理", icon: "host" }
                },
                {
                    path: "/sysdict",
                    name: "数据字典",
                    component: () =>
                        import ("@/pages/modules/yunzhi/sysdict/index"),
                    meta: { title: "数据字典", icon: "host" }
                },
                {
                    path: "/config",
                    name: "参数管理",
                    component: () =>
                        import ("@/pages/modules/yunzhi/config/index"),
                    meta: { title: "参数管理", icon: "host" }
                },
                {
                    path: "/log",
                    name: "系统日志",
                    component: () =>
                        import ("@/pages/modules/yunzhi/log/index"),
                    meta: { title: "系统日志", icon: "host" }
                },
                {
                    path: "/sysapiconfig",
                    name: "对外接口管理",
                    component: () =>
                        import ("@/pages/modules/yunzhi/sysapiconfig/index"),
                    meta: { title: "对外接口管理", icon: "host" }
                },
                {
                    path: "/role",
                    name: "角色管理",
                    component: () =>
                        import ("@/pages/modules/yunzhi/role/index"),
                    meta: { title: "角色管理", icon: "host" }
                },
                {
                    path: "/dept",
                    name: "部门管理",
                    component: () =>
                        import ("@/pages/modules/yunzhi/dept/index"),
                    meta: { title: "部门管理", icon: "host" }
                },
                {
                    path: "/admin",
                    name: "管理员列表",
                    component: () =>
                        import ("@/pages/modules/yunzhi/admin/index"),
                    meta: { title: "管理员列表", icon: "host" }
                }
            ]
        },
        {
            // 模块不存在
            path: "/404",
            component: () =>
                import ("@/pages/404/404")
        },
        { path: "*", redirect: "/404", hidden: true }
    ]
});
// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
    next();
    // let userInfo = sessionStorage.getItem("userInfo");
    // let token = sessionStorage.getItem("token");
    // if (to.matched.some(record => record.meta.requiresAuth)) {
    //   // 判断该路由是否需要登录权限
    //   if (userInfo && token) {
    //     // 登录token
    //     next();
    //   } else {
    //     next({
    //       path: "/"
    //     });
    //     sessionStorage.removeItem("userInfo");
    //     sessionStorage.removeItem("token");
    //   }
    // } else {
    //   // 不要登录权限
    //   if (to.name === "Login" && userInfo && token) {
    //     // 一个浏览器仅允许一个账户登录
    //     next({
    //       path: "/home"
    //     });
    //   } else {
    //     next();
    //   }
    // }
});

router.afterEach((to, from) => {
    if (to.meta.title) {
        document.title = to.meta.title;
    } else {
        document.title = "";
    }
});

export default router;